<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class Adminorder extends CI_Controller {

	var  $temp = 'backend';
	var $error ="";
	public function __construct()
	{
		parent::__construct();
		$this->load->library('parser');
		$this->load->helper('form');
		$this->load->helper(array('form', 'url'));
		$this->load->helper('language');
		$this->load->helper('html');
		$this->load->library('encrypt');
		$this->load->helper('url');
		$this->load->library('image_lib');
		$this->load->library('session');
		$this->load->library('zip');
		$this->load->library('unzip');
	}

	public function index()
	{
		//echo  $this->config->item('base_url').$this->config->item('template') ;
		$data['result']="";
		$this->parser->parse($this->temp.'/'.'login', $data);
	}

	public function login()
	{
		if (!isset($_REQUEST['submit'])) {
			$name = $this->input->post('uname',true);
			$pass = $this->input->post('upass',true);
			$this->load->model('user_model');
			$result = $this->user_model->get_last_one_entries($name,$pass);
			if($result <> 0){
				$_SESSION['userkey'] =  $result; 
				$_SESSION['username'] = $name;
				$_SESSION['datelog'] = date("Y-m-d H:i:s"); 
				$sessionArray = array('logged_in' => TRUE); 
				$_SESSION['logged_in'] = TRUE;
				$this->session->set_userdata($sessionArray);
				redirect('admincp/dashboard');
			}else {
				redirect('admincp/index');
			}
		}
	}

	public function dashboard(){ 
		if(isset($_SESSION['userkey'])){
			$data = array(
            'username' =>  $_SESSION['username'],
			'ipaddress' => $this->session->userdata('session_id'),
			'timelog' =>$_SESSION['datelog'],
			'modules' =>$this->functions_model->getlistfunctions_type(1)
			);
			$this->parser->parse($this->temp.'/'.'dashboard', $data);
		}
		else {
			redirect('admincp/index');
		}
	}

	function logout()
	{
		$this->session->unset_userdata('userkey');
		$this->session->unset_userdata('username');
		$this->session->unset_userdata('logged_in');
		$this->session->unset_userdata('datelog');		
		session_destroy();
		redirect('admincp/index');
	}

	public function charts(){
		$data['modules'] = $this->functions_model->getlistfunctions_type(1);
		if(isset($_SESSION['userkey'])){
			$data = array(
            'username' =>  $this->session->userdata('username'),
			'ipaddress' => $this->session->userdata('session_id'),
			'modules' =>$this->functions_model->getlistfunctions_type(1)
			);
			//echo $this->session->userdata('session_id');
			//echo $this->session->userdata('userkey');
			$this->parser->parse($this->temp.'/'.'dashboard', $data);
		}
		else {
			redirect('admincp/index');
		}
	}

	public function functions(){
		$data['modules'] = $this->functions_model->getlistfunctions_type(1);
		if(isset($_SESSION['userkey'])){
			$this->load->model('functions_model');
			$data = array(
	            'username' =>  $this->session->userdata('username'),
				'ipaddress' => $this->session->userdata('session_id'),
				'flists' => $this->functions_model->getlistfunctions(),
				'modules' =>$this->functions_model->getlistfunctions_type(1)
			);
			$this->parser->parse($this->temp.'/'.'dashboard', $data);
		}
		else {
			redirect('admincp/index');
		}
	}

	public function addfunctions(){
		$data['modules'] = $this->functions_model->getlistfunctions_type(1);
		if(isset($_SESSION['userkey'])){
			$this->load->model('functions_model'); 
			if ($this->input->post('submit')) {
				$namemodules = $this->input->post('modname',true);
				$check = $this->functions_model->checkexit($namemodules );
				if($check == 0){
					$mypath= "./uploads";
					$file = $this->do_upload($mypath,'userfile');
					$chars = explode(".",$file);
					$namemodules = $this->input->post('modname',true);
					$type = $this->input->post('ftype',true);
					if($type==1){
						$this->unzip->extract('uploads/'.$file,'./app/');
						$this->unzip->close();
					}
					elseif($type == 2) {
						$this->unzip->extract('uploads/'.$file,'./app/');
						$this->unzip->close();
					}else {
						
					}
					$insert = $this->functions_model->insert_functions($namemodules,$chars[0],$type);
					redirect('admincp/functions');
				}else{
					$this->error = "Tải file bị lỗi";
					redirect('admincp/addfunctions');
				}
			}
			$data = array(
	            'username' =>  $this->session->userdata('username'),
				'ipaddress' => $this->session->userdata('session_id'),
				'error' =>$this->error,
				'modules' =>$this->functions_model->getlistfunctions_type(1)
			);
			$this->parser->parse($this->temp.'/'.'dashboard', $data);
		}
		else {
			redirect('admincp/index');
		}
	}

	public function users(){ 
		if(isset($_SESSION['userkey'])){
			$this->load->model('functions_model');
			$this->load->model('user_model');
			$data = array(
	            'username' =>  $this->session->userdata('username'),
				'ipaddress' => $this->session->userdata('session_id'),
				'flists' => $this->functions_model->getlistfunctions(),
				'modules' =>$this->functions_model->getlistfunctions_type(1),
				'ulists' =>$this->user_model->get_alluser()
			);
			$this->parser->parse($this->temp.'/'.'dashboard', $data);
		}
		else {
			redirect('admincp/index');
		}
	}
	
	public function deleteuser($id=0)
	{
		if(isset($_SESSION['userkey'])){
			$this->load->model('user_model');
			$this->user_model->deleteuser($id);		
			redirect('admincp/users');
		} else {
			redirect('admincp/index');
		}
		
	}
	
	public function adduser(){
		
		if(isset($_SESSION['userkey'])){
			$this->load->model('functions_model');
			$this->load->model('user_model');
			$username = $this->input->post('username',true);
			$check = $this->user_model->checkexit($username);
			if($check <= 0) {
				$password= $this->input->post('password',true);
				$mypath= "./uploads/avatars";
				$avatar = $this->do_upload_image($mypath,'userimage');
				$utype = $this->input->post('utype',true);
				$key = $this->input->post('rules',true);
				$active = $this->input->post('radio',true);
				$this->user_model->insert_user($username,$password,$active,$key,$avatar,$utype);
			} 
			$data = array(
	            'username' =>  $this->session->userdata('username'),
				'ipaddress' => $this->session->userdata('session_id'),
				'flists' => $this->functions_model->getlistfunctions(),
				'modules' =>$this->functions_model->getlistfunctions_type(1),
				'ulists' =>$this->user_model->get_alluser()
			);
			$this->parser->parse($this->temp.'/'.'dashboard', $data);
		}
		else {
			redirect('admincp/index');
		}
	}

	function do_upload($mypath,$filename){
		// UPload Zip
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'zip';
		$config['max_size']	= '20000';
		$this->load->library('upload', $config);
		if(isset($filename))
		{
			//echo $filename; die();
			if (!$this->upload->do_upload($filename))
			{
				$error = array('error' => $this->upload->display_errors());
				return NULL;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		}else {
			echo $this->upload->display_errors();
		}
	}
	
	function do_upload_image($mypath,$filename){
		// UPload Zip
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'gif|jpg';
		$config['max_size']	= '20000';
		$this->load->library('upload', $config);
		if(isset($filename))
		{
			//echo $filename; die();
			if (!$this->upload->do_upload($filename))
			{
				$error = array('error' => $this->upload->display_errors());
				return NULL;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		}else {
			echo $this->upload->display_errors();
		}
	}

}
